<?php

namespace App\Http\Controllers\Api;

use App\Models\Escort;
use App\Models\Order;
use App\Models\Shop;
use App\Models\UserShop;

class UserController extends Controller
{
    /*
     * 用户信息
     */
    public function userInfo()
    {
        $order_info['today_nums'] = Order::where('status',3)->where('escort_finish_time','>',strtotime(date("Y-m-d")))->where('escort_id',$this->escort_id)->count();
        $order_info['month_nums'] = Order::where('status',3)->where('escort_finish_time','>',strtotime(date("Y-m")))->where('escort_id',$this->escort_id)->count();;
        $all_nums = Escort::where('id',$this->escort_id)->value('counts');
        $order_info['all_nums'] = $all_nums ? $all_nums : 0;

        $this->userInfo['order_info'] = $order_info;
        $this->success($this->userInfo);
    }

    /**
     * 用户地址
     */
    public function userAddress()
    {
        $addresses = $this->userInfo->addresses;
        $this->success($addresses);
    }

    /**
     * 获取用户绑定商家
     */
    public function userShops()
    {
        $shops = $this->userInfo->shops;
        $this->success($shops);
    }

    /**
     * 绑定商户
     */
    public function bindShop()
    {
        $shopId = request()->input('shop_id');

        $userShop = UserShop::whereUid($this->uid)->whereShopId($shopId)->first();
        if(!$userShop){
            UserShop::create([
                'shop_id' => $shopId,
                'uid' => $this->uid
            ]);
        }
        $this->success();
    }

    /**
     * 解绑商户
     * @throws
     */
    public function unbindShop()
    {
        $shopId = request()->input('shop_id');

        $userShop = UserShop::whereUid($this->uid)->whereShopId($shopId)->first();
        if($userShop){
            $userShop->delete();
        }
        $this->success();
    }

    /**
     * 商户列表
     */
    public function shopList()
    {
        $name = request()->input('keywords');
        $builder = Shop::select('id', 'name', 'avatar', 'address', 'phone', 'realname');
        if($name){
            $builder->where(function($query)use($name){
                $query->where('name', 'like', "%{$name}%")
                ->orWhere('phone', $name);
            });
        }
        $shops = $builder->get();
        $userShops = $this->userInfo->shops->pluck('id')->toArray();
        $shops->map(function ($shop)use($userShops){
            $shop->has_bind = false;
            if(in_array($shop->id, $userShops)){
                $shop->has_bind = true;
            }
        });
        $this->success($shops);
    }

    /**
     * 我的订单
     */
    public function myOrders()
    {
        $type = request()->input('type');
        $keywords = request()->input('keywords');
        $query = Order::whereUid($this->uid)->with('school');
        if($type == 1){
            $query->whereStatus(1);
        }elseif($type == 2){
            $query->whereStatus(2);
        }elseif($type == 3){
            $query->whereStatus(3);
        }else{
            $query->whereIn('status', [1,2,3]);
        }
        $query->whereIn('type', [1,2]);
        if($keywords){
            $query->where(function($query)use($keywords){
                $query->where('from_name', 'like', "%{$keywords}%")
                    ->orWhere('from_phone', $keywords)
                    ->orWhere('from_address', 'like', "%{$keywords}%")
                    ->orWhere('to_address', 'like', "%{$keywords}%");
            });
        }
        $list = $query->orderBy('id', 'desc')->paginate(10);
        $list->map(function ($item){
            if($item->escort_id){
                $escort = Escort::whereKey($item->escort_id)->select('id', 'uid', 'realname', 'phone', 'avatar')->first();
                $item->escort = $escort;
            }
        });
        $this->success($list);
    }

    public function otherOrder()
    {
        $keywords = request()->input('keywords');
        $query = Order::whereUid($this->uid)->whereType(3)->whereIn('status', [1,2,3]);

        if($keywords){
            $query->where(function($query)use($keywords){
                $query->where('to_name', 'like', "%{$keywords}%")
                    ->orWhere('to_phone', $keywords)
                    ->orWhere('to_address', 'like', "%{$keywords}%");
            });
        }
        $list = $query->orderBy('id', 'desc')->paginate(10);
        $this->success($list);
    }

    /**
     * 订单详情
     */
    public function orderDetail()
    {
        $orderId = request()->input('order_id');

        $order = Order::whereOrderId($orderId)->with(['school', 'escort'])->first();
        if(!$orderId || !$order){
            return $this->error(-1, '订单错误');
        }

        $this->success($order);
    }

    /**
     * 用户账户
     */
    public function userAccount()
    {
        $totalRecharge = Order::whereType(5)->whereUid($this->uid)->where('status', '>=', 1)->sum('pay_price');
        $totalOrder = Order::whereType(1)->whereUid($this->uid)->where('status', '>=', 1)->sum('pay_price');

        $balance = $this->userInfo->balance;

        $this->success([
            'totalRecharge' => $totalRecharge,
            'totalOrder' => $totalOrder,
            'balance' => $balance,
            'is_leader' => $this->userInfo->is_leader
        ]);
    }

    /**
     * 我的按钮
     */
    public function myMenu()
    {
//        $menus = [
//            [
//                'name' => '我的订单',
//                'type' => 'order',
//                'sort' => 100,
//            ],
//            [
//                'name' => '账户中心',
//                'type' => 'account',
//                'sort' => 200,
//            ],
//        ];
        $escort = Escort::whereKey($this->escort_id)->first();
        if(!$escort){
            $menus[] =
            [
                'name' => '申请镖师',
                'type' => 'apply_escort',
                'sort' => 300,
            ];
        }
//        elseif($escort->status == 0){
//            $menus[] =
//            [
//                'name' => '镖师申请中',
//                'type' => 'escort_applying',
//                'sort' => 300,
//            ];
//        }elseif($escort->status == 1){
//            $menus[] =
//                [
//                    'name' => '镖师中心',
//                    'type' => 'escort_center',
//                    'sort' => 300,
//                ];
//        }
        $menus[] =
            [
                'name' => '用户反馈',
                'type' => 'report',
                'sort' => 400,
            ];
//        $menus[] =
//            [
//                'name' => '设置',
//                'type' => 'setting',
//                'sort' => 500,
//            ];

        $this->success($menus);
    }
}
